package com.mab.mall.search.thread;

import java.util.concurrent.*;

/**
 * 线程基本操作
 *  区别：
 *      继承Thread和实现Runnable接口没有办法获取返回结果，实现Callable接口可以获取线程返回结果
 *      这三种方式都不能控制我们的资源，线程池可以控制资源。
 */
public class ThreadDemo {

    // 定义一个线程池对象
    private static ExecutorService service = Executors.newFixedThreadPool(5);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main方法执行开始================");
        ThreadDemo1 threadDemo1 = new ThreadDemo1();
        threadDemo1.start();

        ThreadDemo2 threadDemo2 = new ThreadDemo2();
        new Thread(threadDemo2).start();

        ThreadDemo3 threadDemo3 = new ThreadDemo3();
        FutureTask futureTask = new FutureTask(threadDemo3);
        new Thread(futureTask).start();
        Object o = futureTask.get();
        System.out.println("callable线程返回结果:"+o);

        // 线程池使用
        service.execute(()-> System.out.println("线程池创建："+Thread.currentThread().getName()));
        System.out.println("main方法执行结束============");
    }

}

class ThreadDemo1 extends Thread{
    @Override
    public void run() {
        System.out.println("Thread线程："+Thread.currentThread().getName());
    }
}

class  ThreadDemo2 implements Runnable{
    @Override
    public void run() {
        System.out.println("实现Runnable线程"+Thread.currentThread().getName());
    }
}

class ThreadDemo3 implements Callable<Integer>{

    @Override
    public Integer call() throws Exception {
        System.out.println("实现Callable线程："+Thread.currentThread().getName());
        return 10;
    }
}